package deepclone.clone;

/**
 * 男孩类
 * 实现Cloneable接口
 */
public class Boy  implements  Cloneable{
    private String name;
    private int age;

    //女朋友 (has a关系)
    private Girl girl;

    public Girl getGirl() {
        return girl;
    }

    public void setGirl(Girl girl) {
        this.girl = girl;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //调用父类的clone方法
        Boy boy = (Boy) super.clone();
        //深克隆 girl对象也重新克隆出来
        boy.girl = (Girl) this.girl.clone();
        return boy;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", girl=" + girl +
                '}';
    }
}